<?php

class mysql {
	protected static $ins = null;
	protected $conn = null;
	
	protected function __construct(){
		$this->conn = new PDO('mysql:dbname=lee;host=localhost','root','root');
		return $this->conn;
	}
	
	protected function __clone(){}
	
	public static function getIns(){
		if(!self::$ins instanceof self){
			self::$ins = new self();
		}
		return self::$ins;
	}
	public function query($sql){
		return $this->conn->query($sql);
	}
	public function getOne($sql){
		$res = $this->conn->prepare($sql);
		$res->execute();
		return $res->fetch()[0];
		
	}
	
	public function getRow($sql){
		$res = $this->conn->prepare($sql);
		$res->execute();
		return $res->fetch(PDO::FETCH_ASSOC);
	}
	
	public function getAll($sql){
		$res = $this->conn->prepare($sql);
		$res->execute();
		return $res->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function getLastId(){
		return $this->conn->lastInsertId();
	}
	/*
	*
	*数据的插入以及修改
	*/
	public function autoExec($table,$array = array(),$act='insert',$where=0){
		if(empty($table) || empty($array)){
			return false;
		}
		if($act == 'insert'){
			$sql = "insert into $table (";
			$sql .= implode(',',array_keys($array));
			$sql .= ") values (";
			foreach($array as $v){
				$sql .= "'" . $v . "',";
			}
			$sql = rtrim($sql,',') . ')';
			// echo $sql;exit;
			$res = $this->conn->prepare($sql);
			return $res->execute();
		}else if($act == 'update'){
			$sql = "update $table set ";
			foreach($array as $k=>$v){
				$sql .= $k . '=' . "'" . $v . "'" . ','; 
			}
			$sql = rtrim($sql,',') . " where " . $where;
			$res = $this->conn->prepare($sql);
			return $res->execute();
		}
	}
	
	public function del($table,$where=0){
		$sql = "delete from $table where " . $where;
		$res = $this->conn->prepare($sql);
		return $res->execute();
	}
	
}

	